package com.david.cms.controller;

import java.io.IOException;
import java.util.Date;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.david.basic.model.Pager;
import com.david.cms.model.Member;
import com.david.cms.service.IMemberService;

/**
 * @description 会员管理后台控制层
 * @author PS
 * @Jul 23, 2012 12:15:33 PM
 */
@RequestMapping("/admin/member_manage")
@Controller
public class MemberManageController {
	private IMemberService memberService;
	@Inject
	public void setMemberservice(IMemberService memberService) {
		this.memberService = memberService;
	}
	
	@RequestMapping(value = "tomanage")
	public ModelAndView tomange(){
		return new ModelAndView("member/member_manage");
	}
	/**
	 *@description 初始化管理界面
	 *@param page 
	 *@param request
	 *@param response
	 *@return
	 *@3:18:02 PM
	 */
	@RequestMapping(value = "initPage")
	public String initPage(Model model) {
		Pager<Member> members=null;
		try {
			 members=this.memberService.searchMemberByPage(null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("datas", members);
		return "member/member_list";
	}
	@RequestMapping(value = "delete/{id}")
	public String del(@PathVariable String id,Model model){
		System.out.println(id+"---------------------------");
		try {
			this.memberService.deleteMember(Integer.valueOf(id));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "member/member_list";
		}
		return "redirect:/admin/member_manage/initPage";
	}
	
	@RequestMapping(value = "show/{id}")
	public String show(@PathVariable String id,Model model){
		Member m=null;
		try {
			m=this.memberService.searchMemberById(Integer.valueOf(id));
			model.addAttribute("showMember",m);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "member/member_list";
		}
		return "member/member_show";
	}
	
	@RequestMapping(value = "toUpdate/{id}")
	public String toUpdate(@PathVariable String id,Model model){
		Member m=null;
		try {
			m=this.memberService.searchMemberById(Integer.valueOf(id));
			model.addAttribute("showMember",m);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "member/member_list";
		}
		return "member/member_update";
	}
	
	@RequestMapping(value = "update",method=RequestMethod.POST)
	public String update(Member member,Model model){		
	
		try {
//			this.memberService.updateMember(member);			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "member/member_update";
		}		
		return "redirect:/admin/member_manage/initPage";
	}	
	@RequestMapping(value = "toAdd")
	public String toAdd(){
	
	
			return "member/add";
	
	
}
	
	@RequestMapping(value = "add")
	public String add(Member member,Model model){
		member.setRegisterDate(new Date());
		System.out.println(member.getRealName()+"=========================");
		try {
			this.memberService.addMember(member);			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "member/member_add";
		}		
		return "redirect:/admin/member_manage/initPage";
	
}
	
	/**
	 * 验证该用户名是否已经被注册
	 */
	@RequestMapping(value = "checkName")
	public void checkMemberByLoginName(String loginID,HttpServletRequest request, HttpServletResponse response) throws IOException{
		  response.setContentType("text/xml;charset=utf-8");
          request.setCharacterEncoding("utf-8");
          response.setCharacterEncoding("utf-8");
		
		Member member=  memberService.searchMemberByLoginName(loginID);
		System.out.println(member==null);
		System.out.println("----------------sadasdsadas----------------"+loginID);
		if(member==null){
			
			response.getWriter().println(1);
		}else{
		
			response.getWriter().println(2);
		}
	
	}
}
